﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;

namespace ODM_Code.Forms
{
    public partial class SelectAnalysisAreaForm : Form
    {
        private ODM_Code.rUtils ru;
        private Dictionary<string, string> extraAreasDict;

        public SelectAnalysisAreaForm()
        {
            InitializeComponent();
            ru = new ODM_Code.rUtils();
            extraAreasDict = new Dictionary<string, string>();
        }


        public void button1_Click(object sender, EventArgs e)
        {
            StreamWriter sw = new StreamWriter("C:\\magfire\\lt_sa.txt");
            string magPath = "C:\\magfire\\";
            string dbfPath = "";
            bool goodSelection = false;

            switch (comboBox1.Text)
            {
                case "":
                    MessageBox.Show("You must select an analysis area!", "Selection Error");
                    break;
                case "Lake Tahoe - North":
                    dbfPath = "DBF_LT_NORTH";
                    if (Directory.Exists(magPath + dbfPath))
                    {
                        ConfigSettings.WriteSetting("analysisArea", dbfPath);
                        sw.Write(dbfPath);
                        goodSelection = true;
                    }
                    break;
                case "Lake Tahoe - East":
                    dbfPath = "DBF_LT_EAST";
                    if (Directory.Exists(magPath + dbfPath))
                    {
                        ConfigSettings.WriteSetting("analysisArea", dbfPath);
                        sw.Write(dbfPath);
                        goodSelection = true;
                    }
                    break;
                case "Lake Tahoe - South":
                    dbfPath = "DBF_LT_SOUTH";
                    if (Directory.Exists(magPath + dbfPath))
                    {
                        ConfigSettings.WriteSetting("analysisArea", dbfPath);
                        sw.Write(dbfPath);
                        goodSelection = true;
                    }
                    break;
                case "Lake Tahoe - West":
                    dbfPath = "DBF_LT_WEST";
                    if (Directory.Exists(magPath + dbfPath))
                    {
                        ConfigSettings.WriteSetting("analysisArea", dbfPath);
                        sw.Write(dbfPath);
                        goodSelection = true;
                    }
                    break;
                case "Huron":
                    dbfPath = "DBF_Huron";
                    if (Directory.Exists(magPath + dbfPath))
                    {
                        ConfigSettings.WriteSetting("analysisArea", dbfPath);
                        sw.Write(dbfPath);
                        goodSelection = true;
                    }
                    break;
                case "Trapper-Bunkhouse":
                    dbfPath = "DBF_Trapper_Bunkhouse";
                    if (Directory.Exists(magPath + dbfPath))
                    {
                        ConfigSettings.WriteSetting("analysisArea", dbfPath);
                        sw.Write(dbfPath);
                        goodSelection = true;
                    }
                    break;
                case "Tutorial":
                    dbfPath = "DBF_TUT_WEST";
                    if (Directory.Exists(magPath + dbfPath))
                    {
                        ConfigSettings.WriteSetting("analysisArea", dbfPath);
                        sw.Write(dbfPath);
                        goodSelection = true;
                    }
                    break;
                default:
                    //MessageBox.Show("Invalid Selection. Try again.", "Select Analysis Area");
                    dbfPath = extraAreasDict[comboBox1.Text];
                    if (Directory.Exists(magPath + dbfPath))
                    {
                        ConfigSettings.WriteSetting("analysisArea", dbfPath);
                        sw.Write(dbfPath);
                        goodSelection = true;
                    }
                    break;

            }

            sw.Close();

            if (goodSelection == true)
            {
                ConfigSettings.WriteSetting("initialised", "true");
                this.Close();
            }
            else
            {
                MessageBox.Show(dbfPath + " does not exist. Please install it and reselect \nor try a different analysis area.");
            }
            
        }

        private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            switch (comboBox1.Text)
            {
                case "Lake Tahoe - North":
                    this.pictureBox1.Image = global::ODM_Code.Properties.Resources.aaNorthc;
                    break;
                case "Lake Tahoe - East":
                    this.pictureBox1.Image = global::ODM_Code.Properties.Resources.aaEastc;
                    break;
                case "Lake Tahoe - West":
                    this.pictureBox1.Image = global::ODM_Code.Properties.Resources.aaWestc;
                    break;
                case "Lake Tahoe - South":
                    this.pictureBox1.Image = global::ODM_Code.Properties.Resources.aaSouthc;
                    break;
                case "Tutorial":
                    this.pictureBox1.Image = global::ODM_Code.Properties.Resources.aaWestc;
                    break;
                case "Trapper-Bunkhouse":
                    this.pictureBox1.Image = global::ODM_Code.Properties.Resources.aaTrapper;
                    break;
                default:
                    this.pictureBox1.Image = null;
                    break;

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //MessageBox.Show("Not Implemented Yet.", "Create New Analysis Area", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            this.Hide();

            ODM_Code.Forms.ManageAnalysisAreasForm maaf = new ManageAnalysisAreasForm();
            maaf.Activate();
            maaf.BringToFront();
            maaf.ShowDialog();

            this.AddAreasToList();
            this.Show();
        }

        private void SelectAnalysisAreaForm_Load(object sender, EventArgs e)
        {
            this.AddAreasToList();
        }

        private void AddAreasToList()
        {
            this.comboBox1.Items.Clear();

            this.comboBox1.Items.Add("Lake Tahoe - North");
            this.comboBox1.Items.Add("Lake Tahoe - East");
            this.comboBox1.Items.Add("Lake Tahoe - South");
            this.comboBox1.Items.Add("Lake Tahoe - West");
            this.comboBox1.Items.Add("Huron");
            this.comboBox1.Items.Add("Trapper-Bunkhouse");
            this.comboBox1.Items.Add("Tutorial");

            string extraAreas = ConfigSettings.ReadSetting("extraAreas");
            string[] splitAreas = extraAreas.Split(';');

            foreach (string s in splitAreas)
            {
                if (s.Length > 0)
                {
                    int divider = s.IndexOf(':');
                    string name = s.Substring(0, divider);
                    string path = s.Substring(divider + 1);
                    extraAreasDict[name] = path;

                    this.comboBox1.Items.Add(name);
                }
            } 
        }

    }
}
